<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 发布操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 * create by allen.qiang
 */

require 'app_class/issueInfoHandler.class.php';

// require_once('projman/release.php');

class pa_issueInfo {
	
	//入口
	public function execute(){
		global $G_PGLOB;
		
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		switch ($operFlg){
			case 1:
				$this->init();
				break;
			case 2:
				$this -> logon();
				break;
			case 3:
				$this->checkLogon();
				break;
			case 4:
				$this->pre_deploy();
				break;
			case 5:
				$this->showList();
				break;
			default:
				$G_PGLOB->smarty->assign('error_msg','参数错误');
				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
				break;
		}
	}
	
	/* 
	 * 页面初始化
	 *  */
	private function init(){
		
		global $G_PGLOB;
		
		//判断用户是否登录
		$getSession = self::is_login();
		
		if (empty($getSession)){
			
			//如果没有设置session就进入登录模版：project/userLogin.tpl
			self::userLogin('/quanliadmin/app/project/issueInfo.pscript?operFlg=1');
			
		}else {
			$username = $G_PGLOB -> sessionCache_get('username');  //当前用户
// 			$username = 'xuyuehua';
			//检查当前用户是否存在
			$userLogin = new entity('s_userlogin');
			$userLogin -> username = $username;
			if (!$G_PGLOB -> DBCA -> fetch($userLogin)) {
				
				$G_PGLOB->smarty->assign('error_msg','密码错误');
				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
				
			}else {
				
				$issueInfoHandler  = new issueInfoHandler($G_PGLOB->DBCA);
					
				$issueInfoHandler->issueUsername = $username;
					
				$versionNumberInfo = $issueInfoHandler->getPersonalIssueID();//个人发布的版本号（下拉框中的版本号）
				if (isset($versionNumberInfo) && $versionNumberInfo['revisions_fail_flg'] != 0){
					$G_PGLOB->smarty->assign('error_msg',$versionNumberInfo['fail_content']);
				}else {
					$G_PGLOB->smarty->assign('error_msg','');
					$G_PGLOB->smarty->assign('versionNumberInfo',$versionNumberInfo['revisions']);
				}
				
				$G_PGLOB->smarty->assign('issueUsername',$username);
				
				$G_PGLOB->smarty->display('project/issueInfo.tpl');
			}
			
		}
		
	}
	
	/**
	 *   用户登录
	 *
	 */
	private function logon(){
		global $G_PGLOB;
		$G_PGLOB -> sessionCache_remove('username');
		if (isset($_REQUEST['return_url'])) {
			$return_url = $_REQUEST['return_url'];
		}else{
			$return_url = '/quanliadmin/login.php';
		}
		$G_PGLOB -> smarty ->assign('return_url',$return_url);
		$G_PGLOB -> smarty -> display('project/userLogin.tpl');
	}
	
	/**
	 * 检查用户登录是否正确与否
	 *
	 */
	private function checkLogon(){
		global $G_PGLOB;
		if (isset($_REQUEST['return_url'])) {
			$return_url = $_REQUEST['return_url'];
		}else{
			$return_url = '/quanliadmin/login.php';
		}
		$G_PGLOB -> smarty -> assign('return_url',$return_url);
	
		//------------------------------------------//
		$userLogin = new entity('s_userlogin');
		if (isset($_REQUEST['username']) && isset($_REQUEST['pwd'])) {  //检查用户名和密码是否通过表单提交
			$user = trim($_REQUEST['username']);
			$pwd = trim($_REQUEST['pwd']);
			$userLogin -> username = $user;
			$userLogin -> pwd = md5($pwd);
			if (!$G_PGLOB -> DBCA -> fetch($userLogin)) {   			//检查用户是否存在
				$G_PGLOB -> smarty -> assign('user_1',$user);
				$G_PGLOB -> smarty -> assign('pwd_1',$pwd);
				$G_PGLOB -> smarty -> assign('error_msg_1','密码错误');
				$G_PGLOB -> smarty ->display('project/userLogin.tpl');
			} else {
				$G_PGLOB->sessionCache_set("username", $user, 24*3600);
				if ($return_url) {		//登录成功后跳转到登录之前页面
					header('location:'.$return_url);
				}else{
					header('location:'.$return_url);
				}
			}
		} else {
			$G_PGLOB -> smarty -> assign('error_msg_1','密码错误');
			$G_PGLOB -> smarty -> display('project/userLogin.tpl');
		}
	}
	
	private function showList(){
		
		global $G_PGLOB;
		
		$issueID = isset($_REQUEST['issueID']) ? trim($_REQUEST['issueID']) : null;
		$versionNumber = isset($_REQUEST['versionNumber']) ? trim($_REQUEST['versionNumber']) : null;
		
		$issueInfoHandler  = new issueInfoHandler($G_PGLOB->DBCA);
		
		$issueInfoHandler->versionNumber = $versionNumber;
		
		$issueInfoHandler->issueID = $issueID;
		
		$result = $issueInfoHandler->get_issue_result();
		
		if ($result['code'] != 200){
			
			$G_PGLOB->smarty->assign('error_msg',$result['message']);
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
			
		}else {
			
			$G_PGLOB->smarty->assign('issueInfo',$result['message']);
			$G_PGLOB->smarty->display('project/issueList.tpl');
			
		}
		
	}
	
	/* 
	 * 将版本号对应的文件发布到页面上
	 *  */
	private function pre_deploy(){
		
		global $G_PGLOB;
		
		$versionNumber = isset($_REQUEST['versionNumber']) ? trim($_REQUEST['versionNumber']) : null;
		
		if (empty($versionNumber) || !is_numeric($versionNumber)){
			
			$G_PGLOB->smarty->assign('error_msg','版本号错误');
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
			
		}else {
			
			$issueInfoHandler  = new issueInfoHandler($G_PGLOB->DBCA);
			
			$issueInfoHandler->versionNumber = $versionNumber;
			
			$result = $issueInfoHandler->pre_deploy_do();
			
			if (!empty($result['error_msg'])){
				
				$G_PGLOB->smarty->assign('error_msg',$result['error_msg']);
				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
				
			}else {
				$G_PGLOB->smarty->assign('error_msg','');
				
				$G_PGLOB->smarty->assign('fileArr',$result['data']);
				
				$G_PGLOB->smarty->assign('versionNumber',$versionNumber);
				
				$G_PGLOB->smarty->display('project/issueFile.tpl');
				
			}
			
		}
	}
	
	/**
	 *  跳转用户登录模版
	 *
	 */
	private function userLogin($url_logined){
		global $G_PGLOB;
		header('location: /quanliadmin/app/project/issueInfo.pscript?operFlg=2&return_url='.$url_logined);
	}
	
	/**
	 *  判断用户是否登录
	 *
	 */
	private function is_login(){
		global $G_PGLOB;
		return $G_PGLOB->sessionCache_get('username');
	}
}